package com.example.news_reader;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class C_Adapter extends BaseExpandableListAdapter{
	private ArrayList<String> groupList = null;   
	private ArrayList<ArrayList<DataList>> childList = null;   
	private LayoutInflater inflater = null;    
	private ViewHolder viewHolder = null;         
	public C_Adapter(
		Context c, ArrayList<String> groupList, ArrayList<ArrayList<DataList>> childList){  
		super();        
		this.inflater = LayoutInflater.from(c);       
		this.groupList = groupList;    
		this.childList = childList;     
	}           
	
	// 그룹 포지션을 반환한다. 
	@Override   
	public String getGroup(int groupPosition) {         
		return groupList.get(groupPosition);     
	}     
	
	// 그룹 사이즈를 반환한다.     
	@Override 
	public int getGroupCount() {        
		return groupList.size();     
	}       
	
	// 그룹 ID를 반환한다.   
	@Override 
	public long getGroupId(int groupPosition) {   
		return groupPosition;   
	}   
	
	// 그룹뷰 각각의 ROW  
	@Override  
	public View getGroupView(int groupPosition, boolean isExpanded,    
			View convertView, ViewGroup parent) {                  
		View v = convertView;                  
		if(v == null){ 
			viewHolder = new ViewHolder();       
			v = inflater.inflate(R.layout.category, parent, false);            
			viewHolder.tv_groupName = (TextView) v.findViewById(R.id.text);
			v.setTag(viewHolder);      
		}else{          
			viewHolder = (ViewHolder)v.getTag();        
		}                  
		viewHolder.tv_groupName.setText(getGroup(groupPosition));            
		return v;     
	}         
	
	// 차일드뷰를 반환한다.  
	@Override  
	public DataList getChild(int groupPosition, int childPosition) {         
		return childList.get(groupPosition).get(childPosition);     
	}      
	
	// 차일드뷰 사이즈를 반환한다.  
	@Override   
	public int getChildrenCount(int groupPosition) {        
		return childList.get(groupPosition).size();    
	}       
	
	// 차일드뷰 ID를 반환한다.
	@Override   
	public long getChildId(int groupPosition, int childPosition) {       
		return childPosition;     
	}  
	
	// 차일드뷰 각각의 ROW   
	@Override 
	public View getChildView(int groupPosition, int childPosition,         
			boolean isLastChild, View convertView, ViewGroup parent) {          
		View v = convertView;      
		if(v == null){
			viewHolder = new ViewHolder();         
			v = inflater.inflate(R.layout.list, null);          
			viewHolder.tv_childName = (TextView) v.findViewById(R.id.tv_tit);
			viewHolder.tv_childDate = (TextView) v.findViewById(R.id.tv_date);
			viewHolder.img_child = (ImageView) v.findViewById(R.id.iv);
			v.setTag(viewHolder);   
		}else{          
			viewHolder = (ViewHolder)v.getTag();  
		}                  
		viewHolder.tv_childName.setText(getChild(groupPosition, childPosition).getName()); 
		viewHolder.tv_childDate.setText(getChild(groupPosition, childPosition).getDate());
		viewHolder.img_child.setImageBitmap(getChild(groupPosition, childPosition).getImg());
		return v;     
		}      
	
	@Override    
	public boolean hasStableIds() { 
		return true; 
	}       
	
	@Override    
	public boolean isChildSelectable(int groupPosition, int childPosition) { 
		return true;
	}          
	
	class ViewHolder {         
		public TextView tv_groupName;         
		public TextView tv_childName;     
		public TextView tv_childDate;     
		public ImageView img_child;     
	}   
} 
